import 'dart:convert';

import '../config/base_configuration.dart';
import '../data/json_common_func.dart';

class ChannelModel {
  ChannelModel({
    required this.id,
    required this.name,
    required this.key,
    required this.beginAt,
    required this.usersCount,
    required this.viewUsersCount,
    required this.isSwitch,
    this.anchoring,
    required this.cover,
    required this.description,
    this.code: "",
    required this.avatar,
    required this.isAnchor,
    required this.interaction,
    required this.gold,
    required this.living_anchor,
    required this.homepage_carousel_title,
    required this.factor,
    required this.longest_minutes,
    this.pullUrl: "",
    this.vertical_cover: "",
    this.announcement: "",
    this.announcement_at: "",
  });

  factory ChannelModel.fromJson(Map<String, dynamic> json) => ChannelModel(
        id: asT<int>(json['id'])!,
        name: asT<String>(json['name'])!,
        avatar: asT<String>(json['avatar'] ?? "")!,
        key: asT<String>(json['key'])!,
        beginAt: asT<String>(json['begin_at'])!,
        usersCount: asT<int>(json['users_count'] ?? 0)!,
        factor: asT<String>(json['factor'] ?? "0")!,
        viewUsersCount: asT<int>(json['view_users_count'])!,
        isSwitch: asT<bool>(json['switch'])!,
        anchoring: json['anchoring'] == null
            ? Anchoring(id: 0, avatar: "", nickname: "")
            : Anchoring.fromJson(asT<Map<String, dynamic>>(json['anchoring'])!),
        cover: asT<String>(json['cover'])!,
        description: asT<String>(json['description']) ?? "",
        vertical_cover: asT<String>(json['vertical_cover']) ?? "",
        announcement: asT<String>(json['announcement']) ?? "",
        announcement_at: asT<String>(json['announcement_at']) ?? "",
        code: asT<String>(json['code']) ?? "",
        isAnchor: asT<bool>(json['is_anchor'] ?? false)!,
        interaction: asT<bool>(json['interaction'] ?? false)!,
        gold: asT<bool>(json['gold'] ?? false)!,
        homepage_carousel_title:
            asT<Map>(json['homepage_carousel_title'] ?? {})!,
        living_anchor: asT<Map>(json['living_anchor'] ?? {})!,
        longest_minutes: asT<String>(json['longest_minutes'] ?? "15.0")!,
        pullUrl:
            StreamConf.PullBaseUrl + json['preview_key'] + VideoFormat.m3u8,
      );

  int id;
  String name;
  String key;
  String beginAt;
  String avatar;
  int usersCount;
  int viewUsersCount;
  bool isSwitch;
  Anchoring? anchoring;
  String cover;
  String description;
  bool isAnchor;
  String code;
  bool interaction;
  Map homepage_carousel_title;
  Map living_anchor;
  bool gold;
  String factor;
  String longest_minutes;
  String vertical_cover;
  String announcement;
  String announcement_at;

  //播放地址
  late String pullUrl;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'key': key,
        'begin_at': beginAt,
        'users_count': usersCount,
        'switch': isSwitch,
        'anchoring': anchoring,
        'cover': cover,
        'description': description,
        'code': code,
        'gold': gold,
        'is_anchor': isAnchor,
        'interaction': interaction,
        'vertical_cover': vertical_cover,
        'factor': factor,
        'homepage_carousel_title': homepage_carousel_title,
        'living_anchor': living_anchor,
        'living_anchor': longest_minutes,
        'announcement': announcement,
        'announcement_at': announcement_at,
      };

  static List<ChannelModel> fromJsonList(List jsonRes) {
    return jsonRes.map((e) => ChannelModel.fromJson(e)).toList();
  }
}

class Anchoring {
  Anchoring({
    required this.id,
    required this.nickname,
    required this.avatar,
  });

  factory Anchoring.fromJson(Map<String, dynamic> json) => Anchoring(
        id: asT<int>(json['id'] ?? 0)!,
        nickname: asT<String>(json['nickname'] ?? "")!,
        avatar: asT<String>(json['avatar'] ?? "")!,
      );

  int id;
  String nickname;
  String avatar;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'nickname': nickname,
        'avatar': avatar,
      };
}
